package com.dodo.blog.ui.page.admin;

import com.dodo.blog.model.Category;
import com.dodo.blog.model.Role;
import com.dodo.blog.request.BasicRequest;
import com.dodo.blog.server.CategoryService;
import com.dodo.blog.ui.ajax.Response;
import com.dodo.blog.ui.ajax.annotation.AjaxRequest;
import com.dodo.blog.ui.authorization.Authorized;
import com.dodo.blog.ui.component.input.Anchor;
import com.dodo.blog.ui.component.input.Button;
import com.dodo.blog.ui.component.repeater.IDataSource;
import com.dodo.blog.ui.component.repeater.OrderDirection;
import com.dodo.blog.ui.component.repeater.Table;
import com.dodo.blog.ui.component.repeater.TableHeader;
import com.dodo.blog.ui.page.AbstractPage;
import com.dodo.blog.ui.util.FormatUtil;

import javax.inject.Inject;
import java.util.List;

/**
 * @author <a href="mailto:pohorelec@comvai.com">Jozef Pohorelec</a>
 */
@Authorized( roles = {Role.ADMIN} )
public class CategoryList
        extends AbstractPage
{
    private static final long serialVersionUID = 1L;

    private static CategoryService categoryService;

    private static final String DELETE_CATEGORY_METHOD = "deleteCategory";

    @AjaxRequest( name = DELETE_CATEGORY_METHOD )
    @SuppressWarnings( value = "unused" )
    public Response deleteCategory()
    {
        categoryService.deleteCategory( getLongParameter( CategoryEdit.CATEGORY_ID ) );

        return Response.redirect( CategoryList.class );
    }

    @Override
    public void onRender()
    {
        setHeader( localize( "header.categoryList" ) );

        Table<Category> table = new Table<Category>( "category-list-table", new CategoryDataSource() )
        {
            private static final long serialVersionUID = 1L;

            @Override
            public TableHeader[] getTableHeaders()
            {
                return new TableHeader[]{
                        new TableHeader( "name", localize( "label.name" ) ),
                        new TableHeader( "normalizedName", localize( "label.normalizedName" ) ),
                        new TableHeader( "createdDate", localize( "label.createdDate" ) ),
                        new TableHeader( "updatedDate", localize( "label.updatedDate" ) ),
                        new TableHeader(),
                };
            }

            @Override
            public void populate( com.dodo.blog.model.Category category )
            {
                addColumn( category.getName() );
                addColumn( category.getNormalizedName() );
                addColumn( category.getCreatedDate(), FormatUtil.DATE_FORMAT_MONTH_YEAR );
                addColumn( category.getUpdatedDate(), FormatUtil.DATE_FORMAT_MONTH_YEAR );
                addColumn(
                        new Anchor( "edit", localize( "button.edit" ), CategoryEdit.class ).addParameter( CategoryEdit.CATEGORY_ID, category.getId() ),
                        new Button( "delete", category.getId().toString(), localize( "button.delete" ) ).setAjaxMethod( DELETE_CATEGORY_METHOD )
                );
            }
        };
        add( table );

        add( new Anchor( "new-category", localize( "button.new" ), CategoryEdit.class ) );
    }

    @Inject
    public void setCategoryService( CategoryService categoryService )
    {
        CategoryList.categoryService = categoryService;
    }

    private class CategoryDataSource
            implements IDataSource<Category>
    {
        private static final long serialVersionUID = 1L;

        @Override
        public List<Category> getRows( int firstRow, int maxRows, String orderBy, OrderDirection direction )
        {
            BasicRequest request = new BasicRequest( firstRow, maxRows );
            request.addSort( orderBy, direction );
            return categoryService.getCategoryList( request );
        }
    }
}